<?php
namespace Admin\Controller;
use Think\Controller;

/**
 * Class ShopController
 * @package Admin\Controller
 * 商家类
 */
class ShopController extends AdminBasicController {

    public $shop = '';
    public function _initialize(){
        $this->checkLogin();
        $this->shop = D('Shop');
        $pro = M("areas")->where(array('parent_id'=>1))->order('sort_order DESC,area_id ASC')->select();
        $this->assign("pro",$pro);
    }
    /**
     * 商家列表
     * @author mss
     * @time 2017-12-13
     */
    public function shopList(){
        //用户名称查找
        if(!empty(trim(I('post.name')))){
            $name = trim(I('post.name'));
            $where['name'] = array('LIKE',"%".$name."%");
            $parameter['name'] = $name;
        }
        //账号查找
        if(!empty(trim(I('post.account')))){
            $phone = trim(I('post.account'));
            $where['account'] = array('LIKE',"%".$phone."%");
            $parameter['account'] = $phone;
        }
        //按申请时间查找
        if(!empty(I('request.start_time')) && !empty(I('request.end_time'))){
            $start_time = I('request.start_time');
            $this->assign("start_time",$start_time);
            $end_time = I('request.end_time');
            $this->assign("end_time",$end_time);
            $w['ctime'] = array(array('EGT',strtotime($start_time)),array('ELT',strtotime($end_time)),'and');
            $parameter['start_time'] = I('request.start_time');
            $parameter['end_time'] = I('request.end_time');
        }

        $shop = $this->shop->selectShop($where,'ctime desc','15',$parameter);


        $this->assign('page',$shop['page']);
        $this->assign('list',$shop['list']);

        //提交的参数传回前台 赋值到搜索表单
        $this->assign('request',$_REQUEST);

        $this->display('shopList');
    }

    /**
     * 编辑商家信息
     * @author mss
     * @time 2017-12-13
     */
    public function editShop(){
        if(!IS_POST){
            $shop_id = I('get.id');
            $shop = M('Shop')->where(array('shop_id'=>$shop_id))->find();
            if(!empty($shop['pic'])){
                $list_img = explode(",",$shop['pic']);
                $shop['list_img'] = $list_img;
            }else{
                $shop['list_img'] = "";
            }

            $this->assign('res',$shop);

            $this->display('editShop');
        }else{
            //查看域名前缀是否被注册
            $is_shop = M('Shop')->where(array('prefix_name'=>$_POST['prefix_name'],'shop_id'=>array('NEQ',$_POST['shop_id']),'status'=>array('NEQ',9)))->getField('shop_id');
            if($is_shop){
                $this->error('域名前缀已存在');
            }
            /**先查看商家的账号是否已经被注册了*/
            $account = D("Shop")->where(array("shop_id"=>$_POST['shop_id']))->getField("account");
            if($account != $_POST['account']){
                $shop_res = M("Shop")->where(array('account'=>$_POST['account'],'status'=>array('not in','1,9')))->getField("account");
                if($shop_res){
                    $this->error("此手机号已经被注册！");
                }
            }
            $data = D("Shop")->create();
            if($data){
                if(I("post.head_pic")){
                    $data['head_pic'] = I("post.head_pic");
                }
                if(I("post.pic")){
                    $string = implode(",",I("post.pic"));
                    $data['pic'] = $string;
                }
                if (get_magic_quotes_gpc()) {
                    $data['content'] = stripslashes($_POST['content']);
                } else {
                    $data['content'] = $_POST['content'];
                }
                if(!empty($_POST['password'])){
                    $data['password'] = md5($_POST['password']);
                }
                $res = D("Shop")->where(array("shop_id"=>$_POST['shop_id']))->limit(1)->save($data);
                if($res){
                    $this->success("修改成功！",U("Shop/shopList"));
                }else{
                    $this->error("修改失败！");
                }
            }else{
                $this->error(D("Shop")->getError());
            }
        }
    }

    /**
     * 添加商家
     * @author mss
     * @time 2017-12-13
     */
    public function addShop(){
        if(!IS_POST){

            $this->display('addShop');
        }else{
            //查看域名是否已经存在
            $is_shop = M('Shop')->where(array('prefix_name'=>$_POST['prefix_name']))->getField('shop_id');
            if($is_shop){
                $this->error('域名前缀已存在');
            }
            /**先查看商家的账号是否已经被注册了*/
            $shop_res = M("Shop")->where(array('account'=>$_POST['account'],'status'=>array('not in','1,9')))->getField("shop_id");
            if($shop_res){
                $this->error("此手机号已经被注册！");
            }
            M("Shop")->startTrans();
            $data = D("Shop")->create();
            if($data){
                if(I("post.head_pic")){
                    $data['head_pic'] = I("post.head_pic");
                }
                if(I("post.pic")){
                    $string_pic = implode(",",I("post.pic"));
                    $data['pic'] = $string_pic;
                }
                if (get_magic_quotes_gpc()) {
                    $data['content'] = stripslashes($_POST['content']);
                } else {
                    $data['content'] = $_POST['content'];
                }
                $data['ctime'] = time();
                $res = M("Shop")->add($data);
                /**为商户创建一个二维码*/

                if($res){
                    M("Shop")->commit();
                    $this->success("添加成功！",U('Shop/shopList'));
                }else{
                    M("Shop")->rollback();
                    $this->error("添加失败！");
                }
            }else{
                $this->error(D("Shop")->getError());
            }
        }
    }


    /**
     * 删除操作
     */
    public function deleteShop(){
        if(empty($_REQUEST['shop_id'])){
            $this->error('您未选择任何操作对象');
        }
        $where['shop_id'] = array('IN',I('request.shop_id'));
        $data['status'] = 9;
        $data['utime'] = time();
        $upd_res = $this->shop->editShop($where,$data);
        if($upd_res){
            //其他删除操作

            $this->success('删除操作成功');
        }else{
            $this->error('删除操作失败');
        }
    }

    /**
     * 商家重置密码
     * @author mss
     * @time 2017-12-15
     */
    public function resetPwd(){
        $id = I('id');
        $w['shop_id'] = $id;
        $data['password'] = md5('123456');
        $data['utime'] = time();
        $res = M('Shop')->where($w)->limit(1)->save($data);
        if($res){
            $this->success('重置密码成功');
        }else{
            $this->error('重置密码失败');
        }
    }


    /**ajax上传图片*/
    public function uploadPic(){
        $pic       = $_POST['pic'];
        $pic_name      = $_POST['pic_name'];
        $temp = explode('.',$pic_name);
        $ext = uniqid().'.'.end($temp);
        $base64    = substr(strstr($pic, ","), 1);
        $image_res = base64_decode($base64);
        $pic_link  = "Uploads/Shop/".date('Y-m-d').'/'.$ext;
        $saveRoot = "Uploads/Shop/".date('Y-m-d').'/';
        /**检查目录是否存在  循环创建目录*/
        if(!is_dir($saveRoot)){
            mkdir($saveRoot, 0777, true);
        }
        $res = file_put_contents($pic_link ,$image_res);
        if($res){
            $ajaxData = array("flag" => "success", "message"=>"上传成功！" );
            $result_data['path'] = $pic_link;
            $ajaxData['data'] = $result_data;
            $this->ajaxReturn(json_encode($ajaxData));
        }else{
            $ajaxData = array("flag" => "error", "message"=>"上传失败","data" => array());
            $this->ajaxReturn(json_encode($ajaxData));
        }
    }

    /**判断这个手机号是否已经被注册*/
    public function isRegister(){
        $w['status'] = array('neq',9);
        if($_POST['shop_id']){
            $w1['shop_id'] = $_POST['shop_id'];
            $w1['status'] = array('neq',9);
            $res = M("Shop")->where($w1)->limit(1)->find();
            if($res['account'] != $_POST['account']){
                $this->ajaxReturn(1);
            }
        }else{
            $w['account'] = $_POST['account'];
            $shop_id = M("Shop")->where($w)->limit(1)->getField("shop_id");
            if($shop_id){
                $this->ajaxReturn(1);
            }else{
                $this->ajaxReturn(0);
            }
        }
    }


    /**
     * ajax获取城市
     */
    public function ajaxCity(){
        $where["parent_id"] = $_GET['parent_id'];
        $data = M("areas")->where($where)->select();
        echo json_encode($data);
    }
    /**
     * ajax获取区域
     */
    public function ajaxArea(){
        $where["parent_id"] = $_GET['parent_id'];
        $data = M("areas")->where($where)->select();
        echo json_encode($data);
    }

    /**删除上传的相册的图片*/
    public function delPhoto(){
        /**判断删除的商家的图片type:1头像 type2商家展示图 3商家的资质证明图*/
        $w['shop_id'] = $_POST['shop_id'];
        $res = M("Shop")->where($w)->limit(1)->find();
        $pic = "";
        if($_POST['type'] == 1){
            $data['head_pic'] = "";
            $pic = M("Shop")->where($w)->limit(1)->save($data);
        }elseif ($_POST['type'] == 2){
            $string1 = explode(",",$res['pic']);
            foreach ($string1 as $k=>$v){
                if($v == $_POST['file_path']){
                    unset($string1[$k]);
                }
            }
            if(!empty($string1)){
                $string_other = implode(',',$string1);
                $data['pic'] = $string_other;
                $pic = M("Shop")->where($w)->limit(1)->save($data);
            }else{
                $data['pic'] = "";
                $pic = M("Shop")->where($w)->limit(1)->save($data);
            }
        }elseif ($_POST['type'] == 3){
            $string2 = explode(",",$res['more_pic']);
            foreach ($string2 as $k=>$v){
                if($v == $_POST['file_path']){
                    unset($string2[$k]);
                }
            }
            if(!empty($string2)){
                $string_other = implode(',',$string2);
                $data['more_pic'] = $string_other;
                $pic = M("Shop")->where($w)->limit(1)->save($data);
            }else{
                $data['more_pic'] = "";
                $pic = M("Shop")->where($w)->limit(1)->save($data);
            }
        }
        $file  = $_POST['file_path'];
        $result = @unlink($file);
        if ($result == false && empty($pic)) {
            $this->ajaxReturn(0);
        } else {
            $this->ajaxReturn(1);
        }
    }

}